<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="商户名称" prop="merchantName">
        <el-input
          v-model="queryParams.merchantName"
          placeholder="请输入商户名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="平台订单编号" prop="platformOrderNo">
        <el-input
          v-model="queryParams.platformOrderNo"
          placeholder="请输入平台订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上游订单编号" prop="upstreamOrderNo">
        <el-input
          v-model="queryParams.upstreamOrderNo"
          placeholder="请输入上游订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下游订单编号" prop="downstreamOrderNo">
        <el-input
          v-model="queryParams.downstreamOrderNo"
          placeholder="请输入下游订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select
            v-model="queryParams.orderStatus"
            placeholder="订单状态"
            clearable
            style="width: 240px"
        >
          <el-option
              v-for="dict in order_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单创建时间" prop="orderTime">
        <el-date-picker clearable
          v-model="queryParams.orderTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择订单创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['payment:c_order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['payment:c_order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['payment:c_order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['payment:c_order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="c_orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="商户编号" align="center" prop="merchantId" />
      <el-table-column label="商户名称" align="center" prop="merchantName" />
      <el-table-column label="商户账号" align="center" prop="merchantAccount" />
      <el-table-column label="平台订单编号" align="center" prop="platformOrderNo" />
      <el-table-column label="上游订单编号" align="center" prop="upstreamOrderNo" />
      <el-table-column label="下游订单编号" align="center" prop="downstreamOrderNo" />
      <el-table-column label="付款金额" align="center" prop="amount" >
        <template #default="scope">
          <span>{{ fun(scope.row.amount) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="平台手续费" align="center" prop="platformCharges" >
        <template #default="scope">
          <span>{{ fun(scope.row.platformCharges) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="orderStatus" >
        <template #default="scope">
          <dict-tag :options="order_status" :value="scope.row.orderStatus" />
        </template>
      </el-table-column>
      <el-table-column label="通知状态" align="center" prop="noticeStatus" >
        <template #default="scope">
          <dict-tag :options="notice_status" :value="scope.row.noticeStatus" />
        </template>
      </el-table-column>
      <el-table-column label="支付状态" align="center" prop="payStatus" >
        <template #default="scope">
          <dict-tag :options="pay_status" :value="scope.row.payStatus" />
        </template>
      </el-table-column>
      <el-table-column label="支付渠道" align="center" prop="paymentChannel" />
      <el-table-column label="订单创建时间" align="center" prop="orderTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['payment:c_order:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['payment:c_order:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改代收订单信息对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="c_orderRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="商户编号" prop="merchantId">
          <el-input v-model="form.merchantId" placeholder="请输入商户编号" />
        </el-form-item>
        <el-form-item label="商户名称" prop="merchantName">
          <el-input v-model="form.merchantName" placeholder="请输入商户名称" />
        </el-form-item>
        <el-form-item label="商户账号" prop="merchantAccount">
          <el-input v-model="form.merchantAccount" placeholder="请输入商户账号" />
        </el-form-item>
        <el-form-item label="平台订单编号" prop="platformOrderNo">
          <el-input v-model="form.platformOrderNo" placeholder="请输入平台订单编号" />
        </el-form-item>
        <el-form-item label="上游订单编号" prop="upstreamOrderNo">
          <el-input v-model="form.upstreamOrderNo" placeholder="请输入上游订单编号" />
        </el-form-item>
        <el-form-item label="下游订单编号" prop="downstreamOrderNo">
          <el-input v-model="form.downstreamOrderNo" placeholder="请输入下游订单编号" />
        </el-form-item>
        <el-form-item label="付款金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入付款金额" />
        </el-form-item>
        <el-form-item label="平台手续费" prop="platformCharges">
          <el-input v-model="form.platformCharges" placeholder="请输入平台手续费" />
        </el-form-item>
        <el-form-item label="支付渠道" prop="paymentChannel">
          <el-input v-model="form.paymentChannel" placeholder="请输入支付渠道" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="C_order">
import { listC_order, getC_order, delC_order, addC_order, updateC_order } from "@/api/payment/c_order";

const { proxy } = getCurrentInstance();
const {order_status, notice_status, pay_status} = proxy.useDict("order_status", "notice_status", "pay_status");


const c_orderList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    merchantId: null,
    merchantName: null,
    merchantAccount: null,
    platformOrderNo: null,
    upstreamOrderNo: null,
    downstreamOrderNo: null,
    amount: null,
    platformCharges: null,
    orderStatus: null,
    orderTime: null,
    noticeStatus: null,
    payStatus: null,
    paymentChannel: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询代收订单信息列表 */
function getList() {
  loading.value = true;
  listC_order(queryParams.value).then(response => {
    c_orderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    merchantId: null,
    merchantName: null,
    merchantAccount: null,
    platformOrderNo: null,
    upstreamOrderNo: null,
    downstreamOrderNo: null,
    amount: null,
    platformCharges: null,
    orderStatus: null,
    orderTime: null,
    noticeStatus: null,
    payStatus: null,
    paymentChannel: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("c_orderRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加代收订单信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getC_order(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改代收订单信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["c_orderRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateC_order(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addC_order(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除代收订单信息编号为"' + _ids + '"的数据项？').then(function() {
    return delC_order(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('payment/c_order/export', {
    ...queryParams.value
  }, `c_order_${new Date().getTime()}.xlsx`)
}

/*保留两位小数*/
const fun = (val) => {
  return Number(val).toFixed(2);
}


getList();
</script>
